//
//  PictureTableViewCell.m
//  BeHappy
//
//  Created by dlios on 15/3/9.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "PictureTableViewCell.h"

@implementation PictureTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubViews];
    }
    return self;
}


- (void)createSubViews
{
    self.contentView.backgroundColor = [UIColor colorWithRed:177.0/255 green:160.0/255 blue:151.0/255 alpha:1.0];

    // 图片
    self.picImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 20, self.frame.size.width - 20, 100)];
    self.picImageView.backgroundColor = [UIColor grayColor];
    // 添加手势 点击图片查看大图
    UITapGestureRecognizer *bigTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    self.picImageView.userInteractionEnabled = YES;
    [self.picImageView addGestureRecognizer:bigTap];
    
    [self addSubview:self.picImageView];
    

    // 文字背景图片
    self.textBackground = [[UIImageView alloc] initWithFrame:CGRectMake(10, self.picImageView.frame.origin.y + self.picImageView.frame.size.height, self.frame.size.width - 20, 100)];
    self.textBackground.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.textBackground];
    
    // 文字
    self.contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, self.picImageView.frame.origin.y + self.picImageView.frame.size.height + 5, self.frame.size.width - 20, 100)];
//    self.contentLabel.backgroundColor = [UIColor whiteColor];
    self.contentLabel.font = [UIFont systemFontOfSize:14];
    self.contentLabel.textAlignment = NSTextAlignmentLeft;
    self.contentLabel.numberOfLines = 0;
    self.contentLabel.lineBreakMode = NSLineBreakByCharWrapping;
    [self addSubview:self.contentLabel];
    
    // 用户头像
    self.profileImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, self.contentLabel.frame.origin.y + self.contentLabel.frame.size.height, 50, 50)];
    // 设置图片圆角
    // 1.设置图片边框为隐藏状态
    self.profileImageView.layer.masksToBounds = YES;
    // 2.设置图片圆角
    self.profileImageView.layer.cornerRadius = 25; // 为imageView大小的一半即为圆
    [self addSubview:self.profileImageView];
    
    // 用户名
    self.userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, self.profileImageView.frame.origin.y + 10, 200, 30)];
//    self.userNameLabel.backgroundColor = [UIColor whiteColor];
    self.userNameLabel.font = [UIFont systemFontOfSize:14];
    self.userNameLabel.textColor = [UIColor grayColor];
    [self addSubview:self.userNameLabel];
    
    // 发布时间
    self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, self.profileImageView.frame.origin.y + 50, self.frame.size.width - 20, 20)];
    self.timeLabel.font = [UIFont systemFontOfSize:12];
    self.timeLabel.textColor = [UIColor grayColor];
    self.timeLabel.textAlignment = NSTextAlignmentRight;
    [self addSubview:self.timeLabel];
}

- (void)setPicModel:(PictureModel *)picModel
{
    if (_picModel != picModel) {
        _picModel = picModel;
    }
    
    // 调整图片大小
    CGFloat picWidth = self.frame.size.width - 20; // 保证宽度一致
    CGFloat scale = picWidth / _picModel.width; // 缩放比例
    CGFloat picHeight = (CGFloat)_picModel.height * scale; // 保证比例不变
    if (picHeight > 667.0) {
        picHeight = 300;
        self.picImageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    
    // 更新坐标
    self.picImageView.frame = CGRectMake(10, 20, self.frame.size.width - 20, picHeight);
    self.textBackground.frame = CGRectMake(10, self.picImageView.frame.origin.y + self.picImageView.frame.size.height, self.frame.size.width - 20, [[self class] heightForText:_picModel.text width:self.frame.size.width - 20] + 70);
//    NSLog(@"selfheight = %f", self.frame.size.height);
//    NSLog(@"picHeight = %f", self.picImageView.frame.size.height);
//    NSLog(@"textheight = %f", self.textBackground.frame.size.height);
    
    self.contentLabel.frame = CGRectMake(20, self.picImageView.frame.origin.y + self.picImageView.frame.size.height + 5, self.frame.size.width - 30, [[self class] heightForText:_picModel.text width:self.frame.size.width - 20]);
    self.profileImageView.frame = CGRectMake(20, self.contentLabel.frame.origin.y + self.contentLabel.frame.size.height + 5, 50, 50);
    self.userNameLabel.frame = CGRectMake(80, self.profileImageView.frame.origin.y + 10, 200, 30);
    self.timeLabel.frame = CGRectMake(30, self.profileImageView.frame.origin.y + 40, self.frame.size.width - 50, 20);
    
    

    NSURL *url = [NSURL URLWithString:picModel.image1]; // 默认为jpg图片
    
    NSString *is_gifStr = [NSString stringWithFormat:@"%@", _picModel.is_gif];
    // 判断是否为gif图片
    if ([is_gifStr isEqualToString:@"1"]) {
    
        url = [NSURL URLWithString:_picModel.image0]; // 使用gif的第一帧图片
        
        self.gifImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.picImageView.frame.origin.x, self.picImageView.frame.origin.y, 62, 62)];
        self.gifImageView .image = [UIImage imageNamed:@"common-gif@2x.png"];
    }else{
        self.gifImageView.hidden = YES;
        [self.gifImageView removeFromSuperview];
        self.playButton.hidden = YES;
    }
    
    
    // picImageView显示图片
    [self.picImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@""] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        // 调整图片大小
        CGFloat picWidth = self.frame.size.width - 20; // 保证宽度一致
        CGFloat scale = picWidth / _picModel.width; // 缩放比例
        CGFloat picHeight = (CGFloat)_picModel.height * scale; // 保证比例不变
        if (picHeight > 667.0) {
            picHeight = 300;
            self.picImageView.contentMode = UIViewContentModeScaleAspectFit;
        }
        
        // 更新坐标
        // 图片
        self.picImageView.frame = CGRectMake(10, 20, self.frame.size.width - 20, picHeight);
        // 播放按钮
        NSString *is_gifStr = [NSString stringWithFormat:@"%@", _picModel.is_gif];
        // 判断是否为gif图片
        if ([is_gifStr isEqualToString:@"1"]) {
            self.playButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [self.playButton setImage:[UIImage imageNamed:@"icon_play.png"] forState:UIControlStateNormal];
            [self.playButton addTarget:self action:@selector(startGifAnimating:) forControlEvents:UIControlEventTouchUpInside];
            self.picImageView.userInteractionEnabled = YES;
            self.playButton.frame = CGRectMake(self.picImageView.frame.size.width / 2 - 55, self.picImageView.frame.size.height / 2 - 55, 110, 110);
            [self.picImageView addSubview:self.playButton];
            [self addSubview:self.gifImageView]; // 添加gif图标
        }else {
            self.playButton.hidden = YES;
            self.gifImageView.hidden = YES;
            [self.gifImageView removeFromSuperview];
        }
        // 文字背景
        self.textBackground.frame = CGRectMake(10, self.picImageView.frame.origin.y + self.picImageView.frame.size.height, self.frame.size.width - 20, [[self class] heightForText:_picModel.text width:self.frame.size.width - 20] + 70);
        // 文字
        self.contentLabel.frame = CGRectMake(20, self.picImageView.frame.origin.y + self.picImageView.frame.size.height + 5, self.frame.size.width - 30, [[self class] heightForText:_picModel.text width:self.frame.size.width - 20]);
        // 用户头像
        self.profileImageView.frame = CGRectMake(20, self.contentLabel.frame.origin.y + self.contentLabel.frame.size.height + 5, 50, 50);
        // 用户名
        self.userNameLabel.frame = CGRectMake(80, self.profileImageView.frame.origin.y + 10, 200, 30);
        // 发布时间
        self.timeLabel.frame = CGRectMake(30, self.profileImageView.frame.origin.y + 40, self.frame.size.width - 50, 20);
        
    }];

    
    
    // 文字
    self.contentLabel.text = _picModel.text;
    
    // 头像
    NSURL *profileUrl = [NSURL URLWithString:_picModel.profile_image];
    [self.profileImageView sd_setImageWithURL:profileUrl placeholderImage:[UIImage imageNamed:@""]];
    
    // 用户名
    self.userNameLabel.text = _picModel.screen_name;
    
    // 发布时间
    self.timeLabel.text = _picModel.created_at;
    
}

// 自适应高度方法
+ (CGFloat)heightForText:(NSString *)text width:(CGFloat)width
{
    CGSize size = CGSizeMake(width, 3000);
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14.0] forKey:NSFontAttributeName];
    CGRect frame = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return frame.size.height;
}

- (void)startGifAnimating:(UIButton *)buton
{
//    NSLog(@"开始");
    self.playButton.hidden = YES;
    self.gifImageView.hidden = YES;
    [self.gifImageView removeFromSuperview];
    buton.hidden = YES;
    [self.picImageView sd_setImageWithURL:[NSURL URLWithString:self.picModel.image1] placeholderImage:[UIImage imageNamed:@""] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        
    }];
    
}

- (void)tapAction:(UITapGestureRecognizer *)tap
{
    // 模态
}


- (void)awakeFromNib {
    // Initialization code+
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
